//
//  LotteryNavTypeView.m
//  WanCaiC8
//
//  Created by QiuXun on 2019/1/12.
//  Copyright © 2019 tangang. All rights reserved.
//

#import "LotteryNavTypeView.h"
#import "LabCell.h"
#import "LotteryTypeModel.h"
#import "NewsLottryTypeModel.h"
#import "GalleryModel.h"
@interface LotteryNavTypeView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

//全部
@property (nonatomic ,strong) UIButton *totalButton;
@property (nonatomic ,strong) UICollectionView *collectionView;
@end
@implementation LotteryNavTypeView


//
//- (instancetype)init{
//    if (self=[super init]) {
//        [self setupUI];
//    }
//    return self;
//}



- (void)setDataArr:(NSArray *)dataArr{
    _dataArr=dataArr;
    
    [self.collectionView reloadData];
}

//-(void)setIsHome:(BOOL)isHome{
//
////    _isHome = isHome;
//
//    [self.collectionView reloadData];
//
//}

- (void)updateRow:(NSInteger)row{
    self.selectLottery=row;
    self.scrollIndex=row;
    
    [self.collectionView reloadData];
//    self.home_TypeView=nil;
}

- (void)setSelectLottery:(NSInteger)selectLottery{
    
    _selectLottery=selectLottery;
    if (selectLottery!=-1) {
        self.scrollIndex = selectLottery;
    }

    if (self.chaneBlock) {
        self.chaneBlock(self.selectLottery);
    }
//        if (self.home_TypeView) {
//            [self.home_TypeView setSelectType:_selectLottery];
//        }
  
    
    [self.collectionView reloadData];
}

- (void)setScrollIndex:(NSInteger)scrollIndex{
    
    if (self.dataArr.count==0) {
        return;
    }
    
    [self layoutIfNeeded];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:scrollIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    
    if ([self.dataArr[indexPath.row] isKindOfClass:[LotteryTypeModel class]]) {
        LotteryTypeModel *model=self.dataArr[indexPath.row];
        
        if (_isHomeEnter) {
            //MARK:
            
            if (self.selectLottery ==-1) {
                self.totalButton.selected = YES;
                self.totalButton.layer.borderColor=DEFAULTLINECOLOR.CGColor;
                
            } else {
                self.totalButton.selected = NO;
                self.totalButton.layer.borderColor=[UIColor clearColor].CGColor;
            }
            
            [cell tg_setBtnTitle:model.Name andIsSelect:self.selectLottery==indexPath.row];
        
        }else{
          
            if (_isNewMaster) {
                
                [cell tg_setBtnTitle:model.Name andIsSelect:indexPath.row==self.selectLottery];
                
                self.totalButton.selected = NO;
                self.totalButton.layer.borderColor=[UIColor clearColor].CGColor;
                
                //重置_isNewMaster
                _isNewMaster =NO;
            }else{
                
                [cell tg_setBtnTitle:model.Name andIsSelect:indexPath.row==self.selectLottery&&self.totalButton.selected ==NO];
            }
            
        }
        
        
        
        
    }else if ([self.dataArr[indexPath.row] isKindOfClass:[NewsTypeModel class]]){
//        NewsTypeModel *model=self.dataArr[indexPath.row];
//        [cell setTitle:model.TypeName andIsSelect:indexPath.row==self.selectLottery];
    }else if ([self.dataArr[indexPath.row] isKindOfClass:[GalleryExtraModel class]]){
        GalleryExtraModel *model=self.dataArr[indexPath.row];
        [cell setTitle:[NSString stringWithFormat:@"第%@期",model.Issue] andIsSelect:indexPath.row==self.selectLottery];
    }else if ([self.dataArr[indexPath.row] isKindOfClass:[NewsLottryTypeModel class]]){
        NewsLottryTypeModel *model=self.dataArr[indexPath.row];
        [cell setTitle:model.LTypeName andIsSelect:indexPath.row==self.selectLottery];
    }
    [cell.labTitle setFont:[UIFont boldSystemFontOfSize:15]];
    
    return cell;
}


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    if ([self.dataArr[indexPath.row] isKindOfClass:[LotteryTypeModel class]]) {
//        return CGSizeMake((SCREEN_WIDTH-40)/6+10, 40);
//    }else if ([self.dataArr[indexPath.row] isKindOfClass:[NewsTypeModel class]]){
//        return CGSizeMake((SCREEN_WIDTH-40)/4+10, 40);
//    }else if ([self.dataArr[indexPath.row] isKindOfClass:[GalleryExtraModel class]]){
//        return CGSizeMake((SCREEN_WIDTH-40)/4+10, 40);
//    }else if ([self.dataArr[indexPath.row] isKindOfClass:[NewsLottryTypeModel class]]){
//        return CGSizeMake((SCREEN_WIDTH-40)/5+10, 40);
//    }
//    return CGSizeMake((SCREEN_WIDTH-40)/4+10, 40);
//}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.selectLottery=indexPath.row;
    
    self.totalButton.selected =NO;
    self.totalButton.layer.borderColor=[UIColor clearColor].CGColor;

    
}


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        WS(weakSelf);

        weakSelf.isHomeEnter = NO;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-100)/3.0, 40)];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
    
        
        
        _collectionView.bounces = NO;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        
        _collectionView.backgroundColor = NAVCOLOR;
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator=NO;
        [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf).with.insets(UIEdgeInsetsMake(0, 100, 0, 0));
            
        }];
        
        
        //MARK:
        UIView *totalView = [[UIView alloc] init];
        totalView.backgroundColor =NAVCOLOR;
        totalView.userInteractionEnabled=YES;
    
        [self addSubview:totalView];
        [totalView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.collectionView.mas_left);
            make.left.top.bottom.equalTo(weakSelf);
        }];
        
        
        UIView *verLine = [[UIView alloc] init];
        verLine.backgroundColor = [UIColor colorWithHexString:@"#b82828"];
        [totalView addSubview:verLine];
        [verLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0.7);
            make.right.top.bottom.equalTo(totalView);
        }];
        
       
        
        
        
        self.totalButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
//        self.totalButton.adjustsImageWhenHighlighted = NO;
        [self.totalButton setBackgroundImage:[TGTools createImageWithColor:[UIColor colorWithHexString:@"#b82828"]] forState:UIControlStateSelected];
        [self.totalButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
        [self.totalButton setTitle:@"全部彩种" forState:UIControlStateNormal];
        
        
        self.totalButton.selected = YES;

        [self.totalButton addTarget:self action:@selector(totalButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.totalButton addTarget:self action:@selector(preventFlicker:) forControlEvents:UIControlEventAllTouchEvents];
        
        [totalView addSubview:self.totalButton];
        [self.totalButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(totalView);
            make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-100)/3.0-10, 30));
        }];
        
        self.totalButton.layer.cornerRadius = 5.0;
        self.totalButton.layer.masksToBounds = YES;
        self.totalButton.layer.borderWidth = 1;
        self.totalButton.layer.borderColor=[UIColor colorWithHexString:@"#b82828"].CGColor;
  
        
        
        UIView *horLine = [[UIView alloc] init];
        horLine.backgroundColor = [UIColor colorWithHexString:@"#b82828"];
        [self addSubview:horLine];
        [horLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.7);
            make.right.top.left.equalTo(self);
        }];
  
        
    }
    return _collectionView;
}

- (void)preventFlicker:(UIButton *)button {
    button.highlighted = NO;
}

-(void)totalButtonClick:(UIButton *)button{
    
    
    button.highlighted = NO;
    button.selected =YES;
    button.layer.borderColor=[UIColor colorWithHexString:@"#b82828"].CGColor;
    
    self.selectLottery=-1;
    
}

-(void)dealloc{
    
    
}
@end
